/**
 * 
 */
package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.model.ConsignmentItem;
import labox.innovation.gameserver.model.ConsignmentManager;
import labox.innovation.gameserver.model.ConsignmentItem.TheShelfTask;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.ConsignmentWarehouse;
import labox.innovation.gameserver.model.itemcontainer.PcInventory;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ConsignmentUIPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.timer.Timer;

/**
 * @author ljsh
 * @date 2010-5-5
 * @version 1.0
 * @description  将下架的物品再上架
 */
public class RequestShelveOfShelfItem extends L2GameClientPacket {
	private static final String _C_REQUESTSHELVEOFSHELFITEMITEM = "_C_RequestShelveOfShelfItem";

	private static String optionName = "Articles Added from Warehouse";

	/**
	 * 物品ID
	 */
	private long _itemid;

	/**
	 * 寄售时长
	 */
	private int _consignmentLength;

	/**
	 * 价格
	 */
	private int _price;

	/**
	 * 价格类型
	 */
	private int _priceType;

	/**
	 * 手续费
	 */
	private int _handlingFee;

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#getType()
	 */
	@Override
	public String getType() {
		return _C_REQUESTSHELVEOFSHELFITEMITEM;
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#readImpl()
	 */
	@Override
	protected void readImpl() {
		_itemid = readLong();
		int t=readByte();
		_price = readInt();
		_priceType = readInt();
		_consignmentLength = ConsignmentManager.getConsgnmentLength(t);
		_handlingFee=ConsignmentManager.calculateHandleFee(t, _price);

	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#runImpl()
	 */
	@Override
	protected void runImpl() {
		FuzePcInstance active = getClient().getActiveChar();
		boolean enough = false;
		if (active == null) {
			return;
		}
		ConsignmentWarehouse house = ConsignmentManager.getConsignmentWarehouse(active.getObjectId());
		ConsignmentItem conitem = house.getConsignmentItem(_itemid);
		if (conitem == null||conitem.isShelve()) {
			active.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_NOTHING_TO_SELL));
			return;
		}
		if (_price > ConsignmentManager.PRICELIMIT) {
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_OUTPRICELIMIT));
			return;
		}
		if(_handlingFee==-1){//当计算出来的手续费是-1时，则是非法操作，直接返回
			return;
		}
		int money = 0;
//		if (_priceType == PcInventory.MONEY_SLIVER_ID) {
//			money = active.getInventory().getMoneySilver();
//		}
		if (_priceType == PcInventory.MONEY_NOTE_ID) {
			money = active.getInventory().getMoneyNote();
		}
//		else if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
//			money = active.getInventory().getMoneyYuanBao();
//		}
		else{
			return;
		}
		if (money >= _handlingFee) {
			enough = true;
		} else {
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_NOMONEY));
			return;
		}
		//执行上架操作
		if (enough) {
//			if (_priceType == PcInventory.MONEY_SLIVER_ID) {
//				active.getInventory().reduceMoneySilver(optionName, _handlingFee, active, null);
//			}
			if (_priceType == PcInventory.MONEY_NOTE_ID) {
				active.reduceMoneyNote(optionName, _handlingFee, null, true);
			}
			if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
				active.reduceMoneyYuanBao(optionName, _handlingFee, null, true);
			}
			//conitem1 = new ConsignmentItem(conitem.getItem(), _consignmentLength, _price, _priceType, true);
			//conitem.set
			conitem.setConsignmentLength(_consignmentLength);
			conitem.setPrice(_price);
			conitem.setPriceType(_priceType);
			conitem.setShelve(true);
			
			Timer timer = new Timer(conitem.getConsignmentLength(), false, new TheShelfTask(conitem));
			conitem.setTimer(timer);
			timer.start();
			ConsignmentManager.shelve(conitem);//物品上架
			active.sendPacket(new ConsignmentUIPacket(active));
			active.sendPacket(new SystemMessage(SystemMessageId.SHELVE_SUCCESS));//发送上架成功消息
		}

	}

}
